/**
 * @author: 子龙
 */
package com.fzl.lotterysystem.service.activitystatus.operator;

import com.fzl.lotterysystem.dao.dataobject.ActivityUserDO;
import com.fzl.lotterysystem.dao.mapper.ActivityUserMapper;
import com.fzl.lotterysystem.service.DTO.ConvertActivityStatusDTO;
import com.fzl.lotterysystem.service.enums.ActivityUserStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Component
public class UserOperator extends AbstractActivityOperator{

    @Autowired
    private ActivityUserMapper activityUserMapper;
    @Override
    public Integer sequence() {
        return 1;
    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        List<Long> userIds=convertActivityStatusDTO.getUserIds();
        Long activityId=convertActivityStatusDTO.getActivityId();
        ActivityUserStatusEnum statusEnum=convertActivityStatusDTO.getTargetUserStatus();

        if(activityId==null || CollectionUtils.isEmpty(userIds) || statusEnum==null){
            return false;
        }
       List<ActivityUserDO> activityUserDOList= activityUserMapper.batchSelectByAUIds(activityId,userIds);

        if(CollectionUtils.isEmpty(activityUserDOList)){
            return false;
        }

        for(ActivityUserDO auDO:activityUserDOList){
            //当前活动状态与传入的状态是否一致，一致就不处理
            if(auDO.getStatus().equalsIgnoreCase(statusEnum.name())){
                return false;
            }
        }
        return true;
    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        List<Long> userIds=convertActivityStatusDTO.getUserIds();
        Long activityId=convertActivityStatusDTO.getActivityId();
        ActivityUserStatusEnum statusEnum=convertActivityStatusDTO.getTargetUserStatus();
        try {
            activityUserMapper.batchUpdateStatus(activityId,userIds,statusEnum.name());
            return true;
        }catch (Exception e){
            return false;
        }
    }
}